package com.scholar.goods.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.scholar.shop.common.*;
import com.scholar.shop.seller.goods.api.GoodsApi;
import com.scholar.shop.seller.goods.domain.*;
import com.scholar.shop.seller.sku.domain.GoodsSkuPageQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品基础信息controller
 * @author kimmy
 **/
@RestController
@RequestMapping("/seller/goods")
public class GoodsInfoController {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(GoodsInfoController.class);

    @Autowired
    private GoodsApi goodsApi;

    /**
     * 分页查询商品列表
     * @param pageQuery 分页查询对象
     * @param bindingResult 校验结果
     * @return 结果集
     * @throws BusinessException 业务异常
     */
    @PostMapping("/queryByPage")
    public CommonRes<Pages<GoodsOpenVO>> queryByPage(@RequestBody @Valid GoodsPageQuery pageQuery, BindingResult bindingResult)
            throws BusinessException {

        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_INVALIDATE,
                    CommonUtil.processErrorString(bindingResult));
        }

        return goodsApi.queryByPage(pageQuery);
    }

    /**
     * 新增商品
     * @param goodsOpenVO 商品信息
     */
    @PostMapping
    public CommonRes<GoodsOpenVO> add(@RequestBody @Valid GoodsOpenVO goodsOpenVO) throws Exception {
        CommonRes<GoodsOpenVO> response = goodsApi.add(goodsOpenVO);
        LOG.info("添加商品的结果为 [{}]", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 获取商品信息
     * @param id 商品id
     */
    @GetMapping("/{id}")
    public CommonRes<GoodsOpenVO> getGoodsInfo(@PathVariable("id") Integer id) throws Exception {
        CommonRes<GoodsOpenVO> response = goodsApi.get(id);
        LOG.info("获取到的商品信息为 [{}]", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 修改商品信息 并上架商品
     * @param goodsOpenVO 商品信息
     */
    @PutMapping("/{id}")
    public CommonRes<String> editGoods(@PathVariable("id") Integer id, @RequestBody GoodsOpenVO goodsOpenVO) throws Exception {
        if (ObjectUtil.isEmpty(id)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        CommonRes<String> response = goodsApi.edit(id, goodsOpenVO);
        LOG.info("修改商品的结果为 [{}]", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 商品下架操作
     * @param goodsIds 要下架的商品的集合
     * @return 处理结果
     * @throws BusinessException 异常信息
     */
    @PutMapping("/{goodsIds}/underCarriageGoods")
    public CommonRes<String> underCarriageGoods(@PathVariable String goodsIds) throws Exception {
        if (StringUtils.isEmpty(goodsIds)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }
        return goodsApi.underCarriageGoods(goodsIds);
    }

    /**
     * 将商品放入回收站
     * @param goodsIds 要下架的商品的集合
     * @return 处理结果
     * @throws BusinessException 异常信息
     */
    @PutMapping("/{goodsIds}/recycle")
    public CommonRes<String> throwRecycle(@PathVariable String goodsIds) throws Exception {
        if (StringUtils.isEmpty(goodsIds)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }
        return goodsApi.throwRecycle(goodsIds);
    }

    /**
     * 将商品从回收站中拿出来
     * @param goodsIds 商品id字符串 多个用逗号分隔
     * @return 处理结果
     * @throws BusinessException 异常信息
     */
    @PutMapping("/{goodsIds}/revertGoods")
    public CommonRes<String> revertGoods(@PathVariable String goodsIds) throws Exception {
        if (StringUtils.isEmpty(goodsIds)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }
        return goodsApi.revertGoods(goodsIds);
    }

    /**
     * 删除商品
     * @param goodsIds 删除商品
     * @return 删除结果
     * @throws BusinessException 异常信息
     */
    @DeleteMapping("/{goodsIds}")
    public CommonRes<String> cleanGoods(@PathVariable("goodsIds") String goodsIds) throws Exception {
        if (StringUtils.isEmpty(goodsIds)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }
        return goodsApi.cleanGoods(goodsIds);
    }

    /**
     * 查询多个商品的基本信息
     * @param request 批量查询商品基础信息的请求对象
     * @return 查询结果
     */
    @PostMapping(value = "/details")
    public CommonRes<List<GoodsSelectorOpenVO>> getGoodsDetail(@RequestBody @Valid GoodsBatchQueryRequest request) {
        return goodsApi.batchGetGoodsDetail(request);
    }

    /**
     * 分页查询库存预警的商品列表
     * @param request 库存预警分页查询请求
     * @return 分页查询结果
     */
    @PostMapping("/queryWarningStockGoodsByPage")
    public CommonRes<Pages<GoodsOpenVO>> queryWarningStockGoodsByPage(
            @RequestBody @Valid WarningGoodsQueryPageRequest request, BindingResult bindingResult)
            throws Exception {
        return goodsApi.queryWarningStockGoodsByPage(request);
    }

    /**
     * 查询SKU列表 GoodsSkuVO
     * @param request 查询条件
     * @return 查询结果
     * @throws Exception 异常信息
     */
    @PostMapping("/skus")
    public CommonRes<Pages<GoodsSelectorOpenVO>> list(@RequestBody GoodsSkuPageQueryRequest request) throws Exception {
        return goodsApi.listSku(request);
    }

}
